package com.hanhanxiaochong.zhongxiangchuanmei.view.main.act;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.BannerBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopListBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopTypeBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.page.PageRecorder;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.ShopPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseLayout;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IShopView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.main.adapter.ShopAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.MsgActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.shop.act.ShopNextActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static it.sephiroth.android.library.imagezoom.ImageViewTouchBase.LOG_TAG;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/5.
 * Desc   :
 */

public class ShopLayout extends BaseLayout<ShopPresenter> implements IShopView {


    @BindView(R.id.et_home_search)
    EditText etHomeSearch;
    @BindView(R.id.img_home_msg)
    ImageView imgHomeMsg;
    @BindView(R.id.tl_shop_type)
    TabLayout tlShopType;
    @BindView(R.id.recyclerview_shop)
    RecyclerView recyclerviewShop;

    private ShopAdapter shopAdapter;
    private boolean isFirst = true;
    private PageRecorder pageRecorder;

    public ShopLayout(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_shop;
    }

    @Override
    protected void initPresenter() {
        mPresenter = new ShopPresenter(this);
    }

    @Override
    protected void init() {
        pageRecorder = new PageRecorder();
        mPresenter.getShopType("", "1");
        mPresenter.getBanner();
        mPresenter.getGoodsList(String.valueOf(pageRecorder.getCurrentPage()));
        List<ShopListBean> list = new ArrayList<>();
        List<BannerBean> bannerList = new ArrayList<>();
        recyclerviewShop.setLayoutManager(new LinearLayoutManager(mContext));
        shopAdapter = new ShopAdapter(mContext, list, bannerList);
        recyclerviewShop.setAdapter(shopAdapter);
    }

    @OnClick(R.id.img_home_msg)

    public void onViewClicked() {
        Intent intent = new Intent(mContext, MsgActivity.class);
        mContext.startActivity(intent);
    }

    @Override
    public void setShopType(List<ShopTypeBean.TypeBean> type) {
        ShopTypeBean.TypeBean typeBean = new ShopTypeBean.TypeBean();
        typeBean.setName("热门推荐");
        typeBean.setId("");
        type.add(0, typeBean);
        tlShopType.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                try {
                    if (!isFirst && tab.getPosition() != 0) {
                        Intent intent = new Intent(mContext, ShopNextActivity.class);
                        mContext.startActivity(intent);
                    }
                } catch (IndexOutOfBoundsException exception) {
                    Log.i(LOG_TAG, "onTabSelected: " + exception.toString());
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        //添加tab
        for (int i = 0; i < type.size(); i++) {
            TextView textView = new TextView(mContext);
            textView.setGravity(Gravity.CENTER);
                textView.setText(type.get(i).getName());
            tlShopType.addTab(tlShopType.newTab().setCustomView(textView));
            int finalI = i;
            textView.setOnClickListener(v -> tlShopType.getTabAt(finalI).select());
        }
        isFirst = false;
    }

    @Override
    public void setGoodsList(List<ShopListBean> data) {
        shopAdapter.setData(data);
    }

    @Override
    public void setBanner(List<BannerBean> data) {
        shopAdapter.setBanner(data);
    }
}
